// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

namespace SnapObjects.Data
{
    public class SqlSelectStatement : SqlQueryStatement
    {
        public SqlSelectStatement()
        {
            this.Select = new SqlClause();
            this.Into = new SqlClause();
            this.GroupBy = new SqlClause();
            this.Having = new SqlClause();
            this.OrderBy = new SqlClause();
            this.Unions = new SqlUnionClause();
        }

        public SqlSelectStatement(bool ignoreDelimitIdentifier) : this()
        {
            this.IgnoreDelimitIdentifier = ignoreDelimitIdentifier;
        }

        public int Skip { get; set; } = -1;

        public int Take { get; set; } = -1;

        public bool IsDistinst { get; set; }

        public SqlClause Select { get; private set; }

        public SqlClause Into { get; private set; }

        public SqlClause GroupBy { get; private set; }

        public SqlClause Having { get; private set; }

        public SqlClause OrderBy { get; private set; }

        public SqlUnionClause Unions { get; private set; }

        public override SqlStatementType StatementType => SqlStatementType.Select;

        public override SqlTokenType Type => SqlTokenType.Statement;

        internal bool IgnoreDelimitIdentifier { get; private set; }

        public override void Clear()
        {
            this.Skip = -1;
            this.Take = -1;
            this.IsDistinst = false;
            this.Select.Clear();
            this.From.Clear();
            this.Where.Clear();
            this.GroupBy.Clear();
            this.Having.Clear();
            this.OrderBy.Clear();
            this.Unions.Clear();
        }

        public override SqlStatement Clone()
        {
            var result = new SqlSelectStatement()
            {
                _text = _text,
                IsDistinst = this.IsDistinst,
                Take = this.Take,
                Skip = this.Skip,
                Select = this.Select.Clone(),
                From = this.From.Clone(),
                Where = this.Where.Clone(),
                GroupBy = this.GroupBy.Clone(),
                Having = this.Having.Clone(),
                Into = this.Into.Clone(),
                OrderBy = this.OrderBy.Clone(),
                Unions = this.Unions.Clone(),
                IgnoreDelimitIdentifier = this.IgnoreDelimitIdentifier
            };

            return result;
        }
    }
}
